using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EntityVFX : MonoBehaviour
{
    [SerializeField] private SpriteRenderer spriteRenderer;

    [Header("----- Hurt -----")]
    [SerializeField] private Material hurtMaterial;
    [SerializeField] private float hurtDuration;
    private Material originalMaterial;
    private Coroutine hurtVFXCoroutine;
    private WaitForSeconds hurtWaitWFX; //设置为全局，避免每次调用协程时创建，减少性能消耗

    private void Awake()
    {
        hurtWaitWFX = new(hurtDuration);
        originalMaterial = spriteRenderer.material;
    }
    private void OnDisable()
    {
        spriteRenderer.material = originalMaterial;
    }
    public void HurtEffect(Vector2 durtDir)
    {
        hurtVFXCoroutine ??= StartCoroutine(HurtEffectCo());
    }

    private IEnumerator HurtEffectCo()
    {
        spriteRenderer.material = hurtMaterial;

        yield return hurtWaitWFX;

        spriteRenderer.material = originalMaterial;

        hurtVFXCoroutine = null;
    }
}
